<?php

/**
 * 首页
 * User: tsang
 * Date: 2017/9/14
 * Time: 20:11
 */

namespace app\pc\Controller;
use think\Controller;

use Wechat\WxPayApi;
class Index extends Base
{
    /*
    *	构造函数
    */
    function __construct(){
        parent::__construct();
    }

    //首页
    public function index(){
        $publicityList = [];
        $noticeList = [];
        $policyList = [];
        $uid = $this->info_user['id'];

        //公示
        $whereb['over_time'] = ['>',time()];
        $whereb['is_display'] = 1;
        $publicityList = db('publicity')
            ->field('id,title,department,over_time,create_time')
            ->where($whereb)
            ->order('create_time','desc')
            ->limit(5)
            ->select();

        //通知
        $wherec['is_display'] = 1;
        $noticeList = db('notice')
            ->field('id,title,department')
            ->where($wherec)
            ->order('create_time','desc')
            ->limit(5)
            ->select();

        //政策文件
        $whered['is_display'] = 1;
        $policyList = db('policy')
            ->field('id,department,title')
            ->where($whered)
            ->order('create_time','desc')
            ->limit(5)
            ->select();

        $service = get_article('服务项目','title,picture,department,id,link',4);
        $dynamicList = get_article('查询','department,title,picture,id',6);
        $partner = get_article('入驻平台','picture,link',7);
        $join_company = get_article('平台资源','picture,link,title',4);
        $company = [];
        $i = 0;

        //热搜
        $hot_new_array = array();
        $hot_search = cache("hot_search");
        $hot_search = json_decode($hot_search,true);
        //将重复的元素值筛选出来
        if($hot_search) {
            $hot_array = array_count_values($hot_search);
            $i = 0;
            foreach ($hot_array as $key => $value) {
                $hot_new_array[$i]['search_name'] = $key;
                $hot_new_array[$i]['search_count'] = $value;
                $i++;
            }
            array_multisort(array_column($hot_new_array, 'search_count'), SORT_DESC, $hot_new_array);
        }
        $hot_new_array = array_slice($hot_new_array,0,5);

        //首页banner
        $banner = db('web_set')->field('banner')->find();

        $this->assign('banner', $banner);
        $this->assign('hot_search', $hot_new_array);
        $this->assign('service', $service);
        $this->assign('dynamicList', $dynamicList);
        $this->assign('publicityList', $publicityList);
        $this->assign('noticeList', $noticeList);
        $this->assign('policyList', $policyList);
        $this->assign('company', $partner);
        $this->assign('join_company', $join_company);
        return $this->fetch();
    }

    public function search()
    {
        $where = '';
        $where_certificate_no = '';
        $where_company_name = '';
        $where_time_batch = '';
        $name = input('title');
        if($name != ''){
            $where['xp.item_name'] = array('like','%'.$name.'%');
            $where_time_batch['xp.time_batch'] = array('like','%'.$name.'%');
            $where_certificate_no['xp.certificate_no'] = $name;
            $where_company_name['xc.name'] = array('like','%'.$name.'%');
        }

        //查询分类名
        $project_type = db('project_type')->select();

        //查询有多少条记录
        $xls_project_count = db('xls_project')->count('id');
        
        $project_result = db('xls_project')->alias('xp')->join('xls_company xc','xc.id = xp.company_id')->join('project_type pt','pt.id = xp.type')->where($where)->whereor($where_certificate_no)->whereor($where_company_name)->whereor($where_time_batch)->field('xp.*,xc.name as company_name,pt.type_name')->order('xp.id desc')->paginate(10);



        $page = $project_result->render();
        $this->assign('project_result',$project_result);
        $this->assign('project_type',$project_type);
        $this->assign('name',$name);

        $this->assign('page',$page);
        $this->assign('xls_project_count', $xls_project_count);
        return $this->fetch("index/search");
    }



}